import { useI18n } from "/@/locales/useI18n";
import type { FormItemRule } from "/@/types/antd";

export enum PatternKey {
  cellphone = "cellphone",
  name = "name",
  email = "email",
  code = "code",
  nameWithoutNum = "nameWithoutNum",
  phone = "phone",
  maxNumber255 = "maxNumber255",
  versionCode = "versionCode",
  serialNumber = "serialNumber",
  contractNumber = "contractNumber",
}

export const PatternObj = {
  [PatternKey.cellphone]: /^1[3456789]\d{9}$/,
  [PatternKey.phone]: /(^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$)|(^1[3456789]\d{9}$)/,
  [PatternKey.name]: /^[a-zA-Z0-9\u4e00-\u9fa5\-_·]+$/,
  [PatternKey.email]: /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/,
  [PatternKey.code]: /^[A-Za-z0-9_-]+$/,
  [PatternKey.nameWithoutNum]: /^[a-zA-Z\u4e00-\u9fa5\-_·]+$/,
  [PatternKey.maxNumber255]: /^(0?|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])$/,
  [PatternKey.versionCode]: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/,
  [PatternKey.serialNumber]: /^[-_a-zA-Z0-9]{6,32}$/,
  [PatternKey.contractNumber]: /^[^\u4e00-\u9fa5]+$/,
};

function getPatternDefaultMes(patternKey: string) {
  const { t } = useI18n();
  switch (patternKey) {
    case PatternKey.cellphone:
      return "请输入正确的手机号码";
    case PatternKey.phone:
      return "请输入正确的电话号码";
    case PatternKey.name:
      return t("components.verify.name");
    case PatternKey.email:
      return t("components.verify.email");
    case PatternKey.code:
      return t("components.verify.code");
    case PatternKey.nameWithoutNum:
      return t("components.verify.name");
    case PatternKey.maxNumber255:
      return "请输入正确的数字";
    case PatternKey.versionCode:
      return "请输入正确的版本号";
    case PatternKey.serialNumber:
      return "请输入正确的序列号";
    case PatternKey.contractNumber:
      return "请输入正确的合同编号";
  }
  return t("components.verify.miss");
}

function getMessage(defaultMessage: string, message?: string, messageName?: string, localKey?: string) {
  const { t } = useI18n();
  if (message) {
    return message;
  }
  if (messageName && localKey) {
    return t(localKey, { name: messageName });
  }
  return defaultMessage;
}

export function getRules({
  pattern,
  patternKeys,
  patternMes,
  required,
  requiredMes,
  messageName,
  selectMessageName,
  max,
  min,
}: {
  //内置正则对象key或正则对象
  pattern?: PatternKey | RegExp;
  patternKeys?: PatternKey[];
  //正则校验失败时提示信息
  patternMes?: string;
  //是否必填
  required?: boolean;
  //必填提示信息
  requiredMes?: string;
  //提示主体 如果不传patternMes，messageName为'用户名称'，则提示信息为'请输入正确的用户名称'
  //如果不传requiredMes，messageName为'用户名称'，则提示信息为'请输入用户名称'
  messageName?: string;
  //提示主体 如果不传patternMes，selectMessageName为'用户名称'，则提示信息为'请选择正确的用户名称'
  //如果不传requiredMes，selectMessageName为'用户名称'，则提示信息为'请选择用户名称'
  selectMessageName?: string;
  //最大长度
  max?: number;
  //最小长度
  min?: number;
}) {
  const rules: FormItemRule[] = [];
  const { t } = useI18n();
  if (required) {
    rules.push({
      required: true,
      message: getMessage(
        t("common.required"),
        requiredMes,
        messageName || selectMessageName,
        messageName ? "components.verify.plzInput" : "components.verify.plzSelect"
      ),
    });
  }
  if (pattern) {
    let patternKey = "";
    if (typeof pattern == "string") {
      patternKey = pattern;
    }
    pattern = patternKey ? PatternObj[patternKey] : pattern;
    if (pattern) {
      if (!patternKey && !patternMes) {
        console.warn("pattern参数若为正则对象，必须同时传入patternMes参数");
      }
      rules.push({
        pattern: pattern as RegExp,
        message: getMessage(
          getPatternDefaultMes(patternKey),
          patternMes,
          messageName || selectMessageName,
          messageName ? "components.verify.plzInputCorrect" : "components.verify.plzSelectCorrect"
        ),
      });
    } else {
      console.warn("pattern参数有误");
    }
  } else if (patternKeys && patternKeys.length > 0) {
    patternKeys.forEach((patternKey) => {
      const pattern = PatternObj[patternKey];
      if (pattern) {
        rules.push({
          pattern: pattern as RegExp,
          message: getMessage(
            getPatternDefaultMes(patternKey),
            patternMes,
            messageName || selectMessageName,
            messageName ? "components.verify.plzInputCorrect" : "components.verify.plzSelectCorrect"
          ),
        });
      }
    });
  }
  if (max) {
    rules.push({
      max,
      message: t("components.verify.max", { max }),
    });
  }
  if (min) {
    rules.push({
      min,
      message: t("components.verify.min", { min }),
    });
  }
  return rules;
}
